import { BASE_URL, TIME_OUT, LOGIN_BASE_URL } from './config'

const token = wx.getStorageSync('token')

class CyRequest {
  constructor(baseUrl, authHeader) {
    this.baseUrl = baseUrl
    this.authHeader = authHeader
  }
  request(url, params, method, isAuth = false, header = {}) {
    const finalHeader = isAuth ? {...header, ...this.authHeader} : header
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.baseUrl + url,
        data: params,
        header: finalHeader,
        method: method,
        timeout: TIME_OUT,
        success: (result) => {
          resolve(result.data)
        },
        fail: (res) => {
          reject(res)
        },
      })
    })
  }

  post(url, params, isAuth = false, header) {
    return this.request(url, params, 'POST', isAuth, header)
  }

  get(url, params, isAuth = false, header) {
    return this.request(url, params, 'GET', isAuth , header)
  }
}

const loginRequest = new CyRequest(LOGIN_BASE_URL, {token})

export default new CyRequest(BASE_URL)
export {
  loginRequest
}